const express = require("express");
const bodyParser = require("body-parser");
const static = express.static;
let  useMdel = require("./models/userModel");
let app= express();

app.use(bodyParser.json());
app.use(static("./views"))

app.set("views",__dirname+"/views")


app.get("/",(req,res)=>{
    res.sendFile(__dirname + "/views/index.html")
})

app.post("/formdata",async (req,res)=>{
    await  useMdel.create(req.body).then(()=>{
        console.log("添加成功")
    }).catch(err=>{
        console.log(err);
    })
   
    
    res.send("返回成功")
})

app.get("/backdata",async (req,res)=>{
    let data = await useMdel.find();
    
    res.send(data)
})

app.get("/sortdata",async (req,res)=>{
    let data = await useMdel.find().sort({age:1});
    res.send(data)
})


app.get("/deldata",async (req,res)=>{
    await useMdel.deleteOne({username:req.query.name});
    let data = await useMdel.find();
    res.json(data)
})



app.listen(8989);